package 面试题31_栈的压入_弹出序列;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 12:44 2021/3/19
 * @Description：
 */
public class Soluton {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();

        int popIndex = 0;
        for (int num : pushed) {
            stack.add(num);

            while (!stack.isEmpty() && stack.peek().equals(popped[popIndex])) {
                stack.pop();
                popIndex += 1;
            }
        }

        return stack.isEmpty();
    }

    @Test
    public void test() {
        int[] pushed = {1, 2, 3, 4, 5};
        int[] poped = {4, 5, 3, 2, 1};

        Soluton soluton = new Soluton();
        soluton.validateStackSequences(pushed, poped);
    }
}
